package com.zyj.exam_app.exam.controller;

import com.zyj.exam_app.context.UserContext;
import com.zyj.exam_app.exam.bean.ExamPaper;
import com.zyj.exam_app.exam.service.ExamService;
import com.zyj.exam_app.testPaper_manager.bean.TestPaperBean;
import com.zyj.exam_app.testPaper_manager.service.TestPaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/exam")
public class ExamController {
    @Autowired
    private ExamService examService;
    @Autowired
    private TestPaperService testPaperService;
    /**
     * 答卷结果列表
     * @return
     */
    @RequestMapping("/examPaperList")
    public String examPaperListOfUser(Model model){
        //查询试卷列表
        List<TestPaperBean> testPaperList = testPaperService.findAllTestPapers();
        model.addAttribute("testPaperList", testPaperList);

        //查询答卷列表
        String userId = UserContext.getCurreentUser().getUserInfo().getId();
        List<ExamPaper> examPaperList = examService.getExamPaperOfUser(userId);
        model.addAttribute("examPaperList", examPaperList);
        return "exam/examPaperList";
    }

    /**
     * 考试界面
     * @param mode
     * @return
     */
    @GetMapping("/doExam/{testPaperId}")
    public String doExamPage(@PathVariable("testPaperId") String testPaperId, Model mode){
        //查询试卷
        TestPaperBean testPaper = testPaperService.findTestPaperById(testPaperId);
        mode.addAttribute("testPaper", testPaper);
        return "/exam/doExam";
    }

    /**
     * 考试提交
     * @return
     */
    @PostMapping("/submitExam")
    @ResponseBody
    public Map<String, Object> submitExam(@RequestBody ExamPaper examPaper){
        System.out.println(examPaper);
        examPaper.setUserId(UserContext.getCurreentUser().getUserInfo().getId());
        examService.submitExam(examPaper);
        Map<String, Object> resultMap = new HashMap<>();

        return resultMap;
    }



}
